const { DataTypes } = require('sequelize');
const { sequelize } = require('../database');
const User = require('./User');

const Category = sequelize.define('Category', {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  userid: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: User,
      key: 'id'
    }
  }
});

// 建立关联关系
User.hasMany(Category, { foreignKey: 'userid' });
Category.belongsTo(User, { foreignKey: 'userid' });

module.exports = Category;